﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;

namespace ToolKitManagerIESiteList.Util
{
    internal static class AssemblyHelper
    {

        #region Assembly Attribute Accessors

        internal const string AppSite = "https://toolkitenterprisemode.codeplex.com";

        [SuppressMessage("Microsoft.Design", "CA1031")]
        internal static string CheckVersion()
        {
            try
            {
                using (var wc = new System.Net.WebClient())
                {
                    var s = wc.DownloadString(@"https://toolkitenterprisemode.codeplex.com/wikipage?title=CheckVersion");
                    var posini = s.IndexOf("***VERSION=");
                    var posfim = -1;
                    if (posini > 0)
                    {
                        posfim = s.IndexOf("***", posini + 11);
                        if (posfim > 0)
                        {
                            var newver = s.Substring(posini + 11, posfim - posini - 11);
                            var vN = new Version(newver);
                            if (vN > Assembly.GetExecutingAssembly().GetName().Version)
                            {
                                return newver;
                            }
                        }
                        else
                        {
                            return null;
                        }
                    }
                    else
                    {
                        return null;
                    } 
                }
            }
            catch
            {
                return null;
            }
            return string.Empty;
        }

        public static string AssemblyTitle
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
                    if (!string.IsNullOrEmpty(titleAttribute.Title))
                    {
                        return titleAttribute.Title;
                    }
                }
                return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }

        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

        public static string AssemblyDescription
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }

        public static string AssemblyProduct
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }

        public static string AssemblyCopyright
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }

        public static string AssemblyCompany
        {
            get
            {
                object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                if (attributes.Length == 0)
                {
                    return "";
                }
                return ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion

    }
    }
